$(function() {
    // 判断是折扣商品还是普通商品
    var id = location.search.split('=')[2];
    if (location.search.split('=')[1] == '1&id') {
        // 折扣商品 
        $.ajax({
            type: "get",
            url: "../lib/discount_detail.php",
            data: {
                "id": id
            },
            dataType: "json",
            success: function(res) {
                var pic = JSON.parse(res.pic);
                var template = `
                <div class="item-img left">
                    <div class="scale">
                        <div class="small">
                            <img src=${pic[1].src} class="smallpic">
                            <div class="movebox"></div>
                        </div>
                        <div class="big">
                            <img src=${pic[1].src} class="bigpic">
                        </div>
                    </div>
                </div>
                <div class="item-info">
                    <h2>${res.title}</h2>
                    <dl class="item-price">
                        <dt>价格</dt>
                        <dd>
                            <em>￥</em>
                            <span class="tm-price">${res.price}</span>
                        </dd>
                    </dl>
                    <p class="kucun">库存:<span>${res.num}+</span></p>
                    <p class="item-num">数量:
                        <input type="text" id="item-num" value="1" title="请输入购买量">
                        <b>
                            <span class="mui-amount-increase"></span>
                            <span class="mui-amount-decrease"></span>
                        </b> 件
                    </p>
                    <ul class="item">
                        <span>商品描述：</span>
                        <li>${res.details}</li>
                    </ul>
                    <ul class="item">
                        <span>商品期限：${res.time}</span>
                    </ul>
                    <a href="javasript:;" class="item-cart">加入购物车</a>
                </div>
                `
                $('.details-item').append(template).find('.item-cart').on('click', function() {
                    if (cookie.get('username')) {
                        additem(1, res.id, res.price, $('#item-num').val());
                    } else {
                        alert('请先登录！');
                        $('.unlogin a').trigger('click');
                    }
                });
            }
        });
    } else if (location.search.split('=')[1] == '2&id') {
        // 常规商品
        $.ajax({
            type: "get",
            url: "../lib/product_detail.php",
            data: {
                "id": id
            },
            dataType: "json",
            success: function(res) {
                var pic = JSON.parse(res.pic);
                var template = `
                <div class="item-img left">
                    <div class="scale">
                        <div class="small">
                            <img src=${pic[1].src} class="smallpic">
                            <div class="movebox"></div>
                        </div>
                        <div class="big">
                            <img src=${pic[1].src} class="bigpic">
                        </div>
                    </div>
                </div>
                <div class="item-info">
                    <h2>${res.title}</h2>
                    <dl class="item-price">
                        <dt>价格</dt>
                        <dd>
                            <em>￥</em>
                            <span class="tm-price">${res.price}</span>
                        </dd>
                    </dl>
                    <p class="kucun">库存:<span>${res.num}+</span></p>
                    <p class="item-num">数量:
                        <input type="text" id="item-num" value="1" title="请输入购买量">
                        <b>
                            <span class="mui-amount-increase"></span>
                            <span class="mui-amount-decrease"></span>
                        </b> 件
                    </p>
                    <ul class="item">
                        <span>商品描述：</span>
                        <li>${res.details}</li>
                    </ul>
                    <ul class="item">
                        <span>商品期限：<b>${res.time}</b></span>
                    </ul>
                    <ul class="item">
                        <span>商品类型：<b>${res.type}</b></span>
                    </ul>
                    <a href="javasript:;" class="item-cart">加入购物车</a>
                </div>
                `
                $('.details-item').append(template).find('.item-cart').on('click', function() {
                    if (cookie.get('username')) {
                        additem(2, res.id, res.price, parseInt($('#item-num').val()));
                    } else {
                        alert('请先登录！');
                        $('.unlogin a').trigger('click');
                    }
                });
            }
        });
    }

    // function additem(type, id, price, num) {
    //     var shop = cookie.get('shop'); //获取cookie
    //     var product = {
    //         "type": type,
    //         "id": id,
    //         "price": price,
    //         "num": num
    //     };

    //     if (shop) {
    //         shop = JSON.parse(shop);
    //         if (shop.some(elm => elm.id == id && elm.type == type)) {
    //             shop.forEach((elm, i) => {
    //                 (elm.id == id && elm.type == type) ? elm.num = num: null;
    //             });
    //         } else {
    //             shop.push(product);
    //         }
    //         cookie.set('shop', JSON.stringify(shop), 1);
    //     } else {
    //         shop = [];
    //         shop.push(product);
    //         cookie.set('shop', JSON.stringify(shop), 1);
    //     }
    //     if (confirm('添加成功！是否前往购物车查看?')) {
    //         location.href = "./shop.html";
    //         // 解决默认事件，然上一行代码有效
    //         event.returnValue = false;
    //     }
    // }
    // 添加数量
    $('.details-item').on('click', '.mui-amount-increase', function() {
        $('#item-num').val($('#item-num').val() - 0 + 1);
    });
    // 减少数量
    $('.details-item').on('click', '.mui-amount-decrease', function() {
        if ($('#item-num').val() > 1) {
            $('#item-num').val($('#item-num').val() - 1);
        } else {
            alert('最少为1件!');
        }
    });
});